package com.anuo.app.modules.feed.service;

import com.anuo.app.common.globalhandle.AppException;
import com.anuo.app.common.persistence.PageData;
import com.anuo.app.common.utils.DateUtil;
import com.anuo.app.modules.coach.entity.Coach;
import com.anuo.app.modules.coach.entity.CoachFilter;
import com.anuo.app.modules.feed.dao.FeedDao;
import com.anuo.app.modules.feed.entity.*;
import com.anuo.app.modules.system.entity.Fileinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FeedService {
    @Autowired
    FeedDao feedDao;


    public void saveFeed(Feed feed) {
        feed.preInsert();
        //插入主记录
        feed.setComment_total(0);
        feed.setSupport_total(0);
        feedDao.insertFeed(feed);
        Integer idint = feed.getIdint();
        //插入文件信息
        saveFeedFileList(feed.getId(), feed.getFileinfoList());
        //插入时间线表
        FeedTimeline feedTimeline = new FeedTimeline();
        feedTimeline.preInsert();
        feedTimeline.setFeed_id(feed.getId());
        feedTimeline.setUserid(feed.getUserid());
        feedDao.insertFeedTimeline(feedTimeline);
    }


    @Transactional(readOnly = false)
    public void saveFeedFileList(String master_id, List<Fileinfo> fileinfoList) {
        for (Fileinfo fileinfo : fileinfoList) {
            fileinfo.preInsert();
            fileinfo.setMaster_id(master_id);
            fileinfo.setMaster_type("");
            fileinfo.setFilepath("");
            fileinfo.setSize(0L);
            feedDao.insertFeedFileinfo(fileinfo);
        }
    }

    @Transactional(readOnly = false)
    public void deleteFeed(Feed feed) {
        feedDao.deleteFeed(feed);
        feedDao.deleteFeedFiles(feed);
    }

    @Transactional(readOnly = false)
    public void saveFeedCommnet(FeedComment feedComment) {
        feedComment.preInsert();
        feedComment.setSupport_total(0);
        feedDao.insertFeedCommnet(feedComment);
        feedDao.addFeedCommentTotalOne(feedComment);
    }

    @Transactional(readOnly = false)
    public void deleteFeedComment(FeedComment feedComment) {
        feedDao.deleteFeedComment(feedComment);
        feedDao.subFeedCommentTotalOne(feedComment);
    }



    @Transactional(readOnly = false)
    public void addFeedCommentSupportTotalOne(FeedCommentSupport feedCommentSupport) {

        feedDao.addCommentSupportTotalOne(feedCommentSupport);
    }

    @Transactional(readOnly = false)
    public void saveFeedCommentSupport(FeedCommentSupport feedCommentSupport) {
        //判断是否点赞
        FeedFilter feedFilter = new FeedFilter();
        feedFilter.setUserid(feedCommentSupport.getUserid());
        feedFilter.setFeed_commentid(feedCommentSupport.getFeed_commentid());
        if (feedDao.isExistCommentSupport(feedFilter)) {
            throw new AppException("你已点赞不能再点赞");
        }
        //插入点赞
        feedCommentSupport.preInsert();
        feedDao.insertFeedCommentSupport(feedCommentSupport);
        //点赞总数加一
        feedDao.addCommentSupportTotalOne(feedCommentSupport);
    }

    @Transactional(readOnly = false)
    public void deleteFeedCommentSupport(FeedCommentSupport feedCommentSupport) {
        FeedFilter feedFilter = new FeedFilter();
        feedFilter.setUserid(feedCommentSupport.getUserid());
        feedFilter.setFeed_commentid(feedCommentSupport.getFeed_commentid());
        if (feedDao.isExistCommentSupport(feedFilter)) { //已点赞
            //删除点赞
            feedDao.deleteFeedCommentSupport(feedCommentSupport);
            //点赞总数减一
            feedDao.subCommentSupportTotalOne(feedCommentSupport);
        }

    }

    @Transactional(readOnly = false)
    public void saveFeedSupport(FeedSupport feedSupport) {
        //判断是否点赞
        FeedFilter feedFilter = new FeedFilter();
        feedFilter.setFeedid(feedSupport.getFeedid());
        feedFilter.setUserid(feedSupport.getUserid());
        if (feedDao.isExistFeedSupport(feedFilter)) {
            throw new AppException("你已点赞不能再点赞");
        }
        //插入点赞
        feedSupport.preInsert();
        feedDao.insertFeedSupport(feedSupport);
        //点赞总数加一
        feedDao.addFeedSupportTotalOne(feedSupport);
    }

    @Transactional(readOnly = false)
    public void deleteFeedSupport(FeedSupport feedSupport) {
        FeedFilter feedFilter = new FeedFilter();
        feedFilter.setUserid(feedSupport.getUserid());
        feedFilter.setFeedid(feedSupport.getFeedid());
        if (feedDao.isExistFeedSupport(feedFilter)) { //已点赞
            //删除点赞
            feedDao.deleteFeedSupport(feedSupport);
            //点赞总数减一
            feedDao.subFeedSupportTotalOne(feedSupport);
        }

    }


    @Transactional(readOnly = true)
    public PageData<Feed> GetHotFeedList(FeedFilter feedFilter) {
        PageData<Feed> pageData = new PageData<>();
        feedFilter.setHotMaxDateTime(DateUtil.addDaysToCurrentTime(90));
        PageData.setTotalRow(pageData, feedFilter, () -> feedDao.getHotFeedListCount(feedFilter));
        PageData.setEntities(pageData, feedDao.getHotFeedList(feedFilter));
        return pageData;
    }


    @Transactional(readOnly = true)
    public PageData<FeedComment> getFeedCommentList(FeedFilter feedFilter) {
        PageData<FeedComment> pageData = new PageData<>();
        PageData.setTotalRow(pageData, feedFilter, () -> feedDao.getFeedCommentListCount(feedFilter));
        PageData.setEntities(pageData, feedDao.getFeedCommentList(feedFilter));
        return pageData;
    }

    @Transactional(readOnly = true)
    public PageData<FeedComment> getHotFeedCommentList(FeedFilter feedFilter) {
        PageData<FeedComment> pageData = new PageData<>();
        PageData.setTotalRow(pageData, feedFilter, () -> feedDao.getHotFeedCommentListCount(feedFilter));
        PageData.setEntities(pageData, feedDao.getHotFeedCommentList(feedFilter));
        return pageData;
    }

    @Transactional(readOnly = false)
    public void savePersonFollow(FeedFollow feedFollow) {
        //先判断是否已经关注了
        FeedFilter feedFilter = new FeedFilter();
        feedFilter.setUserid(feedFollow.getUserid());
        feedFilter.setFollowed_userid(feedFollow.getFollowed_userid());

        if (isFollowedThisPerson(feedFilter)) {
            throw new AppException("你已关注了此用户, 不能再关注");
        }
        //然后再添加
        feedFollow.preInsert();
        feedDao.insertFeedFollow(feedFollow);
    }

    @Transactional(readOnly = true)
    public PageData<Feed> getFollowedPersonFeedList(FeedFilter feedFilter) {
        PageData<Feed> pageData = new PageData<>();
        PageData.setTotalRow(pageData, feedFilter, () -> feedDao.getFollowedPersonFeedListCount(feedFilter));
        PageData.setEntities(pageData, feedDao.getFollowedPersonFeedList(feedFilter));
        return pageData;
    }

    @Transactional(readOnly = true)
    public boolean isFollowedThisPerson(FeedFilter feedFilter){
        return feedDao.isFollowedThisPerson(feedFilter);
    }



}


